package com.juyu.miliao.api.dao.mysql;

import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.juyu.miliao.api.domain.UserWallet;
import com.juyu.miliao.common.dataSource.tool.operate.Operate;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Repository
public class UserWalletDao {
	@Autowired
	private SqlSession sqlSession;
	
	private final Logger logger = LoggerFactory.getLogger("userWalletRecord");


	
	@Operate("strategy('byUserId', #id)")
	public UserWallet getUserWalletById(long id){
		return sqlSession.selectOne("getUserWalletById", id);
	}

	@Operate("strategy('byUserId', #entity.userId)")
	public int reviseDiamond(UserWallet entity){
		logger.info("用户id："+entity.getUserId()+",增加钻石："+entity.getDiamond());
		return sqlSession.update("reviseDiamond", entity);
	}

	@Operate("strategy('byUserId', #entity.userId)")
	public int minusDiamond(UserWallet entity){
		logger.info("用户id："+entity.getUserId()+",减少钻石："+entity.getDiamond());
		return sqlSession.update("minusDiamond", entity);
	}

	@Operate("strategy('byUserId', #entity.userId)")
	public int reviseDeposit(UserWallet entity){
		logger.info("用户id："+entity.getUserId()+",增加金币："+entity.getDeposit());
		return sqlSession.update("reviseDeposit", entity);
	}

	@Operate("strategy('byUserId', #entity.userId)")
	public int minusDeposit(UserWallet entity){
		logger.info("用户id："+entity.getUserId()+",减少金币："+entity.getDeposit());
		return sqlSession.update("minusDeposit", entity);
	}

	@Operate("strategy('byUserId', #entity.userId)")
    public void updateRechargeAmount(UserWallet entity) {
		sqlSession.update("updateRechargeAmount",entity);
    }



	@Operate("strategy('byUserId', #userId)")
    public int updateBeans(long userId, long beans) {
		logger.info("用户id："+userId+",剩余："+beans);
		Map<String, Object> map = new HashMap<>(4);
		map.put("userId", userId);
		map.put("beans", beans);
		return sqlSession.update("updateBeans",map);

    }
	@Operate("strategy('byUserId', #userId)")
	public  int addBeans(long userId, long beans) {
		logger.info("用户id："+userId+",剩余："+beans);
		Map<String, Object> map = new HashMap<>(4);
		map.put("userId", userId);
		map.put("beans", beans);
		return  sqlSession.update("addBeans",map);
	}

	@Operate("strategy('byUserId', #entity.userId)")
	public int plusUserWalletBeans(UserWallet entity) {
		return sqlSession.update("plusUserWalletBeans",entity);
	}

	/**
	 * 更新寻币与寻豆
	 * @param entity
	 * @return
	 */
	@Operate("strategy('byUserId', #entity.userId)")
	public int plusUserWalletBeansAndDeposit(UserWallet entity){
		return sqlSession.update("plusUserWalletBeansAndDeposit",entity);
	}
}
